while(condition) { statements ---------- ---------- } next statements
class demo { public static void main(String args[]) { int i=1; while(i<=10) { System.out.print(i+" "); i++; } } }
1 2 3 4 5 6 7 8 9 10
class demo { public static void main(String args[]) { int i=10; while(i>=1) { System.out.print(i+" "); i--; } } }
10 9 8 7 6 5 4 3 2 1
class demo { public static void main(String args[]) { int i=1; while(i<=100) { System.out.print(i+" "); i=i+2; } } }
class demo { public static void main(String args[]) { int i=1; while(i<=100) { if(i%2==0||i%3==0) System.out.print(i+" "); i++; } } }
class demo { public static void main(String args[]) { int i=1; while(i<=10) { System.out.print("Amravati"); i++; } } }
Amravati Amravati Amravati Amravati Amravati Amravati Amravati Amravati Amravati Amravati
import java.util.Scanner; class demo { public static void main(String args[]) { int i=1,s=0; Scanner scan=new Scanner(System.in); while(i<=10) { System.out.print("Enter a number"); int n=scan.nextInt(); s=s+n; i++; } System.out.print("Sum is "+s); } }
Enter a number 5 Enter a number 6 Enter a number 2 Enter a number 9 Enter a number 3 Enter a number 4 Enter a number 1 Enter a number 7 Enter a number 8 Enter a number 3 Sum is 48
import java.util.Scanner; class demo { public static void main(String args[]) { int i=1,s1=0,s2=0; Scanner scan=new Scanner(System.in); while(i<=10) { System.out.print("Enter a number"); int n=scan.nextInt(); if(n%2==0) s1=s1+n; else s2=s2+n; i++; } System.out.print("Sum of even is"+s1); System.out.print("Sum of odd is"+s2); } }
Enter a number 5 Enter a number 6 Enter a number 2 Enter a number 9 Enter a number 3 Enter a number 4 Enter a number 1 Enter a number 7 Enter a number 8 Enter a number 3 Sum of even nos is 20 Sum of odd nos is 28